// 这个数据，是用于：添加（修改）管理员时，设定权限
// 1、定义全部的菜单数据
export const menudata = [
    {
        id: "1",
        label: "首页",
        path: "/admin/home"
    },
    {
        id: "2",
        label: "商品管理",
        children: [
            {
                id: "2-1",
                label: "商品列表",
                path: "/admin/shoplist"
            },
            {
                id: "2-2",
                label: "商品分页",
                path: "/admin/shoppage"
            },
            {
                id: "2-3",
                label: "商品推荐",
                path: "/admin/shopreco"
            }
        ]
    },
    {
        id: "3",
        label: "轮播图管理",
        children: [
            {
                id: "3-1",
                label: "轮播图列表",
                path: "/admin/list"
            },
            {
                id: "3-2",
                label: "轮播图添加",
                path: "/admin/add"
            }
        ]
    },
    {
        id: "4",
        label: "管理员",
        children: [
            {
                id: "4-1",
                label: "管理员列表",
                path: "/admin/adminlist"
            },
            {
                id: "4-2",
                label: "管理员添加",
                path: "/admin/adminadd"
            }
        ]
    },
    {
        id: "5",
        label: "购物车管理",
        children: [
            {
                id: "5-1",
                label: "购物车列表",
                path: "/admin/cart"
            }
        ]
    },
    {
        id: "6",
        label: "订单管理",
        children: [
            {
                id: "6-1",
                label: "订单修改",
                path: "/admin/orderedit"
            },
            {
                id: "6-2",
                label: "订单列表",
                path: "/admin/orderlist"
            }
        ]
    },
    {
        id: "7",
        label: "用户管理",
        children: [
            {
                id: "7-1",
                label: "用户列表",
                path: "/admin/user"
            }
        ]
    },
    {
        id: "8",
        label: "数据分析",
        children: [
            {
                id: "8-1",
                label: "图表分析",
                path: "/admin/analyse"
            }
        ]
    }
]
// 2、根据当前用户的权限，产生相应的菜单
export default function getMenus() {
    //  1）、获取该用户的权限（登录时已经保存了）
    let checkedKeys: string[] = []
    let currKeys = sessionStorage.getItem("grants");
    if (currKeys) {
        checkedKeys = JSON.parse(currKeys)
    }
    if (checkedKeys.length === 0) {
        checkedKeys = ['1', '2', '2-1', '2-2', '2-3', '3', '3-1', '3-2', '4', '4-1', '4-2', '5', '5-1', '6', '6-1', '6-2', '7', '7-1', '8', '8-1'];
    }
    // 2）、根据该用户的权限结合全部菜单数据，产生该用户的菜单
    interface IMenuItem {
        id: string,
        label: string,
        path?: string,
        children?: IMenuItem[]
    }
    let currMenuData: IMenuItem[] = [];//存储当前用户的菜单项
    menudata.forEach(item => {
        if (checkedKeys.indexOf(item.id) > -1) {
            //定义一个菜单项：            
            const newmenuItem = { ...item };
            // 处理子一级
            if (item.children) {
                newmenuItem.children = [];
                item.children.forEach(sonItem => {
                    if (checkedKeys.indexOf(sonItem.id) > -1) {
                        newmenuItem.children?.push({ ...sonItem });
                    }
                })
            }
            currMenuData.push(newmenuItem);
        }
    });
    console.log("currMenuData", currMenuData);
    return currMenuData;
}